package com.obaamilos.msstore;

import java.util.ArrayList;
import java.util.List;

import com.hp.hpl.jena.query.QuerySolution;
import com.hp.hpl.jena.query.ResultSet;
import com.hp.hpl.jena.rdf.model.Literal;
import com.hp.hpl.jena.rdf.model.RDFNode;

public class ResultadoQuery {
	private List<String> cabecalho;
	private List<Linha> registros;
	
	private class Linha {
		private List<String> campos;

		public Linha() {
			this.campos = new ArrayList<String>();
		}
		
		public void addCampo(String pCampo) {
			campos.add(pCampo);
		}
		
		public String getCampo(int pPosicao) {
			return (campos.get(pPosicao));
		}
		
		public String linhaCampo() {
			return (campos.toString());
		}
	}
	
	public ResultadoQuery(ResultSet rs) {
		this.cabecalho = new ArrayList<String>();
		this.registros = new ArrayList<Linha>();
		int i;
		for (i=0; i<rs.getResultVars().size(); i++)
			cabecalho.add(rs.getResultVars().get(i));
		
		String mreg = "";
		List<String> mregistro = new ArrayList<String>();
		
		for (; rs.hasNext(); ) {
			QuerySolution soln = rs.next();
			for (i=0; i<cabecalho.size(); i++) {
				RDFNode nd = soln.get(cabecalho.get(i));       // Get a result variable by name.

				if ( nd.isLiteral() ) {
		        	mreg = ((Literal)nd).getLexicalForm();
		        }
			    //if ( nd.isResource() ) {
			    //    Resource r = (Resource)nd ;
			    //    if ( ! r.isAnon() )
			    //      {
			    //    	mLinha += "uri " + r.getURI();
			    //      }
			    //}
		        else {
		        	mreg = nd.toString();
		        }
				int position = mreg.lastIndexOf("#");
				if (position > 0) {
				 	mregistro.add(mreg.substring(position + 1));
				}
				else {
					mregistro.add(mreg);
				}
			}
			this.addRegistro(mregistro);
			mregistro.clear();
		}
	}
	
	public void addCabecalho(List<String> pCabe) {
		cabecalho.clear();
		for (int i=0; i<pCabe.size(); i++)
			cabecalho.add(pCabe.get(i));
	}
	
	public void addItemCabecalho(String pCabec) {
		cabecalho.add(pCabec);
	}
	
	public String getItemCabecalho(int pPosicao) {
		return (cabecalho.get(pPosicao));
	}
	
	public String linhaCabecalho() {
		return (cabecalho.toString());
	}
	
	public void addRegistro(List<String> pReg) {
		Linha ml = new Linha();
		for (int i=0; i<pReg.size(); i++)
			ml.addCampo(pReg.get(i));
		registros.add(ml);
	}
	
	public String linhaRegistro(int pPosicao) {
		return(registros.get(pPosicao).linhaCampo());
	}
	
	public String valorCampo(int pReg, int pCampo) {
		return (this.registros.get(pReg).getCampo(pCampo));
	}
	
	@Override public String toString() {
		String mResul = this.linhaCabecalho() + System.getProperty("line.separator");
		
		if (this.qtdRegistros() == 0) {
			mResul += "Nenhum registro";
		}
		else {
			for (int i=0; i<this.qtdRegistros(); i++) {
				mResul += this.linhaRegistro(i) + System.getProperty("line.separator");
			}
		}
		
		return (mResul);
	}
	
	public int qtdRegistros() {
		return (this.registros.size());
	}
	
	public void setaResultadoQuery(ResultSet rs) {
		
	}
}
